package cn.com.guage.dtm.server;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class PDFServer {
    private static final int PORT = 8888;
    private static final String PDF_DIR = "pdfs/";

    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(PORT)) {
            System.out.println("服务器已启动，等待客户端连接...");
            while (true) {
                try (Socket socket = serverSocket.accept();
                     ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
                     ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream())) {

                    String command = (String) in.readObject();
                    String fileName = (String) in.readObject();

                    switch (command) {
                        case "view":
                            viewPDF(fileName, out);
                            break;
                        case "edit":
                            String text = (String) in.readObject();
                            editPDF(fileName, text);
                            out.writeObject("编辑成功");
                            break;
                        case "save":
                            savePDF(fileName);
                            out.writeObject("保存成功");
                            break;
                    }
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void viewPDF(String fileName, ObjectOutputStream out) throws IOException {
        File file = new File(PDF_DIR + fileName);
        if (file.exists()) {
            try (FileInputStream fis = new FileInputStream(file);
                 ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = fis.read(buffer)) != -1) {
                    bos.write(buffer, 0, bytesRead);
                }
                byte[] fileBytes = bos.toByteArray();
                out.writeObject(fileBytes);
            }
        } else {
            out.writeObject(null);
        }
    }

    private static void editPDF(String fileName, String text) {
        try (PDDocument document = PDDocument.load(new File(PDF_DIR + fileName))) {
            PDPage firstPage = document.getPage(0);
            try (PDPageContentStream contentStream = new PDPageContentStream(document, firstPage, PDPageContentStream.AppendMode.APPEND, true, true)) {
                contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
                contentStream.beginText();
                contentStream.newLineAtOffset(25, 700);
                contentStream.showText(text);
                contentStream.endText();
            }
            document.save(PDF_DIR + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void savePDF(String fileName) {
        // 这里只是简单示意，实际可根据需求添加更多逻辑
        System.out.println("保存文件: " + fileName);
    }
}    